package gui;

import data.BankDatabase;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import java.awt.*;
import java.util.Arrays;

public class ChoiceAcc extends JPanel{
	
	private JLabel lbl_Header;
	private JButton btn_Logout;
	private JComboBox comboBox_Accounts;
	private JButton btn_Login;
	private JLabel lbl_Intro;
	private JButton btn_UserMenu;
	private JLabel lbl_Info;
	private JTextPane textPane_CurrentUser;
	
	private BankDatabase bdb;
	private Screen screen;
	
	public ChoiceAcc(BankDatabase bdb, Screen screen) {
		
		this.bdb = bdb;
		this.screen = screen;
		
		setPreferredSize(new Dimension(550, 400));
		setLayout(null);

		lbl_Header = new JLabel("Choose an Account");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(173, 11, 243, 34);
		add(lbl_Header);

		btn_Logout = new JButton("Logout");
		btn_Logout.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Logout.setBounds(50, 294, 100, 40);
		add(btn_Logout);
		
		comboBox_Accounts = new JComboBox();
		comboBox_Accounts.setFont(new Font("SansSerif", Font.PLAIN, 16));
		comboBox_Accounts.setBounds(95, 160, 321, 54);
		addAllAcc();
		add(comboBox_Accounts);
		
		btn_Login = new JButton("Login");
		btn_Login.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Login.setBounds(427, 168, 100, 40);
		add(btn_Login);
		
		lbl_Intro = new JLabel("Which Account do you want to enter?");
		lbl_Intro.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Intro.setBounds(95, 120, 286, 21);
		add(lbl_Intro);
		
		btn_UserMenu = new JButton("Enter User Menu");
		btn_UserMenu.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_UserMenu.setBounds(367, 296, 150, 40);
		add(btn_UserMenu);
		
		lbl_Info = new JLabel("For managing all your accounts");
		lbl_Info.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Info.setBounds(277, 264, 241, 21);
		add(lbl_Info);
		
		textPane_CurrentUser = new JTextPane();
		SimpleAttributeSet attributes = new SimpleAttributeSet();
		attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
		StyleConstants.setFontSize(attributes, 16);
		StyleConstants.setFontFamily(attributes, "SansSerif");
		StyleConstants.setAlignment(attributes , StyleConstants.ALIGN_CENTER);  
		textPane_CurrentUser.setParagraphAttributes(attributes,true); 
		textPane_CurrentUser.setEditable(false);
		textPane_CurrentUser.setOpaque(false);
		textPane_CurrentUser.setBackground(new Color(0,0,0,0));
		textPane_CurrentUser.setBounds(202, 56, 178, 60);
		add(textPane_CurrentUser);
	}
	
	public void addActionListener(ActionListener e){
		btn_Logout.addActionListener(e);
		btn_UserMenu.addActionListener(e);
		btn_Login.addActionListener(e);
		comboBox_Accounts.addActionListener(e);
	}
	
	public void showField(int CPR_ID, String Fname, String Lname){
		textPane_CurrentUser.setText(CPR_ID + "\n" + Fname
				+ " " + Lname);
	}
	
	public JButton getBack(){
		return btn_Logout;
	}
	
	public JButton getLogin(){
		return btn_Login;
	}
	
	public JButton getUserMenu(){
		return btn_UserMenu;
	}
	
	public JComboBox getComboBox() {
		return comboBox_Accounts;
	}

	public void addAllAcc(){
		comboBox_Accounts.removeAllItems();	
		String account[] = bdb.getClassUser().getChanger();
		for(int i = 0; i < account.length;i++){
			comboBox_Accounts.addItem(account[i]);		
		}
		screen.pack();
	}
	
	public int getSpecificAccNr(){
		int[] first = new int[bdb.getClassUser().getAccountList().size()];
	    for (int i = 0; i < bdb.getClassUser().getAccountList().size(); i++) {
			first[i] = bdb.getClassUser().getAccountList().get(i).getAccountNumber();
		}
	    return first[comboBox_Accounts.getSelectedIndex()];
	}
	
	public String getSpecificAccType() {
		String[] first = new String[bdb.getClassUser().getAccountList().size()];
	    for (int i = 0; i < bdb.getClassUser().getAccountList().size(); i++) {
			first[i] = bdb.getClassUser().getAccountList().get(i).getAccountType();
		}
	    return first[comboBox_Accounts.getSelectedIndex()];
	}
	
} // end class ChoiceAcc